package cn.zou.mvc;

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletRequestAttributeListener;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

/**
 * 参数注入工具类
 * @author zou
 *
 */
public class InjectionUtils {
public Object[] injection(HttpServletRequest request, Class<?>[] parameterTypes, String[] pramasName,HttpServletResponse response) throws NumberFormatException, ClassNotFoundException, ParseException, IllegalAccessException, InvocationTargetException, InstantiationException {
	if(parameterTypes.length!=pramasName.length)return null;
	Object[] oprams=new Object[parameterTypes.length];
	//获取到请求的提中的所有参数
	//不会对字符进行处理 后期添加此功能
	Map<String, String[]> parameterMap = request.getParameterMap();
	for(int i=0;i<parameterTypes.length;i++){
		Class clazz=parameterTypes[i];
		if(clazz.isArray()){
			//获取组元素的类型
			Class componentType = clazz.getComponentType();
			Object[] newInstance = (Object[]) Array.newInstance(componentType, parameterMap.get(pramasName[i]).length);
			System.arraycopy(parameterMap.get(pramasName[i]), 0, newInstance, 0, newInstance.length);
			oprams[i]=newInstance;
		  }else if(clazz==Class.forName("java.lang.String")){
        	   String string = parameterMap.get(pramasName[i]).toString();
			oprams[i]=string;
          }else if(clazz==Class.forName("java.lang.Integer")){
        	  oprams[i]=Integer.valueOf(parameterMap.get(pramasName[i])[0]);
          }else if(clazz==Class.forName("java.lang.Double")){
        	  oprams[i]=Double.valueOf(parameterMap.get(pramasName[i])[0]);
          }else if(clazz==Class.forName("java.lang.Long")){
        	  oprams[i]=Long.valueOf(parameterMap.get(pramasName[i])[0]);
          }else if(clazz==Class.forName("java.lang.Float")){
        	  oprams[i]=Float.valueOf(parameterMap.get(pramasName[i])[0]);
          }else if(clazz==Class.forName("java.lang.Boolean")){
        	  oprams[i]=Boolean.valueOf(parameterMap.get(pramasName[i])[0]);
          }else if(clazz==Class.forName("java.util.Date")){
              SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              Date valueDate=simpleDateFormat.parse(parameterMap.get(pramasName[i])[0].toString());
              oprams[i]=valueDate;
          }else{
        	  //判断该类是否reuest 或者respose
        	  if(clazz.getName().equals("javax.servlet.http.HttpServletRequest"))oprams[i]=request;
        	  else if(clazz.getName().equals("javax.servlet.http.HttpServletResponse"))oprams[i]=response;
        	  else{
        		  Object newInstance = clazz.newInstance();
        		  BeanUtils.populate(newInstance, parameterMap);
        		  oprams[i]=newInstance;
        	  } 	  
          }
	}
	return oprams;
	}
}
